package nancal.mp.mvc.admin


import io.swagger.annotations.Api
import nbcp.base.annotation.*
import nbcp.base.annotation.mvc.*
import nbcp.base.comm.JsonResult
import nbcp.base.enums.HttpMethod
import nbcp.base.extend.*
import nbcp.base.json.*
import nbcp.base.model.HttpInvoke
import nbcp.base.utils.ResourceUtil
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletResponse

@Api(description = "后台角色", tags = arrayOf("AdminRole"))
@RestController
@RequestMapping("/admin/es")
class AdminSystemController {


    data class EsMappingDataModel(
        var url: String = "",
        var name: String = "",
        var content: String = ""
    )

    @PostMapping("/drop/index")
    fun dropEsIndex(
        @org.springframework.web.bind.annotation.RequestBody mapping: EsMappingDataModel,
        response: HttpServletResponse
    ): JsonResult {
        //创建空的 index: curl -X PUT /{index}
        var http = HttpInvoke();
        http.url = ("${mapping.url}/${mapping.name}");
        http.requestMethod = HttpMethod.DELETE.name
        var res = http.doInvoke();
        if( res.isError){
            return JsonResult.error(res.responseText.AsString(res.status.toString()  ))
        }

        return JsonResult()
    }


    @PostMapping("/index")
    fun createEsIndex(
        @org.springframework.web.bind.annotation.RequestBody mapping: EsMappingDataModel,
        response: HttpServletResponse
    ): JsonResult {
        //创建空的 index: curl -X PUT /{index}
        var http = HttpInvoke()
        http.url = ("${mapping.url}/${mapping.name}");
        http.requestContentType = "application/json"
        http.requestMethod = HttpMethod.PUT.name

        var res = http.doInvoke();
        if( res.isError){
            return JsonResult.error(res.responseText.AsString(res.status.toString()  ))
        }

        return JsonResult()
    }

    @PostMapping("/mapping")
    fun createEsMapping(
        @org.springframework.web.bind.annotation.RequestBody mapping: EsMappingDataModel
    ): JsonResult {
        //curl -X PUT -H 'Content-Type: application/json' '/{index}/_mapping'  -d 'json'
        var http = HttpInvoke()
        http.url = ("${mapping.url}/${mapping.name}/_mapping");
        http.requestContentType = "application/json"
        http.requestMethod = HttpMethod.PUT.name
        http.postString = mapping.content
        var res = http.doInvoke();
        if( res.isError){
            return JsonResult.error(res.responseText.AsString(res.status.toString() ))
        }

        return JsonResult();
    }

    @PostMapping("/mapping/sys-log")
    fun save(
        @Require url: String,
        @Require indexName: String
    ): JsonResult {
        var content = ResourceUtil.readResourceContentText("public/sys-log.txt")
        return createEsMapping(EsMappingDataModel(url, indexName, content))
    }
}